﻿<ResourceDictionary 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:DenisVuyka.Controls.PropertyGrid"  
  xmlns:converters="clr-namespace:DenisVuyka.Controls.PropertyGrid.Converters"
  xmlns:design="clr-namespace:Microsoft.Windows.Design.PropertyEditing;assembly=Microsoft.Windows.Design"
  xmlns:controls="clr-namespace:DenisVuyka.Controls.PropertyGrid.Controls"
  xmlns:data="clr-namespace:DenisVuyka.Controls.PropertyGrid.Data">
  
  <converters:NullOrEmptyStringConverter x:Key="nullOrEmptyStringConverter"/>
  <converters:ExpanderHeaderWidthConverter x:Key="expanderHeaderWidthConverter"/>
  <converters:EmptyValueConverter x:Key="emptyValueConverter"/>
  <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>

  <SolidColorBrush x:Key="PropertyGridTitle" Color="#FFFFFFFF"/>
  
  <LinearGradientBrush x:Key="PropertyGridBackground" StartPoint="0.5,0" EndPoint="0.5,1.0">
    <GradientStop Color="#FF232323" Offset="0"/>
    <GradientStop Color="#FF000000" Offset="1"/>
    <GradientStop Color="#FF393939" Offset="0.48"/>
  </LinearGradientBrush>
  
  <SolidColorBrush x:Key="HeaderBorderBrush" Color="#FF161616"/>

  <LinearGradientBrush x:Key="HeaderBackgroundBrush" StartPoint="0.508,0.107" EndPoint="0.508,0.893">
    <GradientStop Color="#FF2B2B2B" Offset="0"/>
    <GradientStop Color="#FF4E4E4E" Offset="1"/>
    <GradientStop Color="#FF474747" Offset="0.519"/>
  </LinearGradientBrush>
  
  <SolidColorBrush x:Key="PropertiesBackgroundBrush" Color="#FF333333"/>

  <Style TargetType="{x:Type local:PropertyGrid}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:PropertyGrid}">
          <Grid Background="{StaticResource PropertyGridBackground}">
            <DockPanel LastChildFill="True" Margin="2">
              <Border 
                DockPanel.Dock="Top" 
                Width="Auto" 
                Height="Auto"
                CornerRadius="5,5,0,0" 
                BorderThickness="1,1,1,1" 
                BorderBrush="{StaticResource HeaderBorderBrush}" 
                HorizontalAlignment="Stretch" 
                VerticalAlignment="Stretch"
                Background="{StaticResource HeaderBackgroundBrush}">
                
                <StackPanel>
                  <TextBlock 
                    Text="Properties" 
                    Width="Auto" 
                    Height="Auto" 
                    Foreground="{StaticResource PropertyGridTitle}" 
                    Margin="8" 
                    HorizontalAlignment="Left" 
                    VerticalAlignment="Center" />

                  <Grid Visibility="{TemplateBinding ShowPropertyFilter, Converter={StaticResource booleanToVisibilityConverter}}">
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="*"/>
                      <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <TextBox 
                      Grid.Column="0"
                      Margin="2"
                      Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PropertyFilter, UpdateSourceTrigger=PropertyChanged}"
                      ToolTip="Enter search string"/>                    
                    <Button Width="70" Grid.Column="1" Margin="2" Content="Clear" Command="local:PropertyGridCommands.ResetFilter"/>
                  </Grid>
                </StackPanel>
              </Border>

              <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
                <ItemsControl x:Name="PART_items"
                              ItemsSource="{TemplateBinding local:PropertyGrid.Categories}"
                              Background="{StaticResource PropertiesBackgroundBrush}" />
              </ScrollViewer>
            </DockPanel>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="CategoryContainerStyle" TargetType="{x:Type controls:CategoryContainer}">
    <Setter Property="Category" Value="{Binding}" />    
    <Style.Triggers>
      <Trigger Property="PropertiesMatchFilter" Value="false">
        <Setter Property="Visibility" Value="Collapsed"/>
      </Trigger>
    </Style.Triggers>    
  </Style>
  
  <Style x:Key="PropertyContainerStyle" TargetType="{x:Type design:PropertyContainer}">
    <Setter Property="ToolTip" Value="{Binding Path=PropertyEntry.ToolTip, RelativeSource={RelativeSource Self}}" />
    <Setter Property="Visibility" Value="{Binding Path=MatchesFilter, Converter={StaticResource booleanToVisibilityConverter}}"/>
    <Setter Property="InlineRowTemplate">
      <Setter.Value>
        <ControlTemplate>
          <Grid Margin="4">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" Margin="0,0,8,0" TextAlignment="Right" VerticalAlignment="Center" 
                       Text="{Binding Mode=OneTime, Path=DisplayName}">
            </TextBlock>
            <ContentControl 
              x:Name="PART_content" 
              Grid.Column="1" 
              VerticalAlignment="Center" 
              Content="{Binding RelativeSource={RelativeSource Self}, Path=(design:PropertyContainer.OwningPropertyContainer).(design:PropertyContainer.PropertyEntry).PropertyValue, Mode=OneWay}"
              ContentTemplate="{Binding RelativeSource={RelativeSource Self}, Path=(design:PropertyContainer.OwningPropertyContainer).InlineEditorTemplate, Mode=OneWay}"/>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  
  <DataTemplate DataType="{x:Type local:CategoryItem}">
    <controls:CategoryContainer Style="{StaticResource CategoryContainerStyle}">
      <Expander Header="{Binding CategoryName}" IsExpanded="{Binding Category.IsExpanded, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type controls:CategoryContainer}}}">
        
        <ItemsControl x:Name="PART_items" ItemsSource="{Binding Path=Properties}" Grid.Row="0">
          <ItemsControl.ItemTemplate>
            <DataTemplate>
              <design:PropertyContainer Style="{StaticResource PropertyContainerStyle}" PropertyEntry="{Binding}"/>
            </DataTemplate>
          </ItemsControl.ItemTemplate>
        </ItemsControl>

      </Expander>
    </controls:CategoryContainer>
  </DataTemplate>

</ResourceDictionary>